﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace TD.TheGame
{
    public class CastleShooterArrow : Sprite
    {

        Random random = new Random();
        float velocity = 0.1f;
        float speedX = 2f;
        Rectangle[] arrowUp;
        Rectangle[] arrowDown;
        float speedY;

        public CastleShooterArrow(Game game, Vector2 position, bool active, int maxLoopTime, float speedY, SpriteEnumState state) 
            : base(game, position, active, maxLoopTime, state)
        {
            spriteEffects = SpriteEffects.FlipHorizontally;
            arrowUp = animationHandler.animationRec(Environment.CurrentDirectory + "\\XML\\" + "CastleShooter\\" + "castlesupportArrowUP.xml");
            arrowDown = animationHandler.animationRec(Environment.CurrentDirectory + "\\XML\\" + "CastleShooter\\" + "castlesupportArrowDOWN.xml");
            current = arrowUp;
            this.speedY = speedY;
            if (state == SpriteEnumState.Left)
            {
                speedX = random.Next(5, 7);
                speedY = random.Next(6, 8);
                velocity = 0.2f;
            }
            else
            {
                speedX = random.Next(3,5);
                speedY = random.Next(6,8);
                velocity = 0.2f;
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (Active)
            {
                if (speedY < 0)
                {
                    current = arrowDown;
                }
                Move();
                CollisionDetection();
                base.Update(gameTime);
            }
        }

        public void Move()
        {
            position.X += (float)speedX;
            position.Y -= (float)speedY;
            speedY -= velocity;
        }

        public void CollisionDetection()
        {
            if (position.X + BoundingRectangle.Width > Game.Window.ClientBounds.Width || position.X < 0)
            {
                Reset();
            }
            if (position.Y < 0 || position.Y + BoundingRectangle.Height > Game.Window.ClientBounds.Width)
            {
                Reset();
            }

        }

        protected override Texture2D getTexture()
        {
            //Init texture
            manager.LoadAsset("level\\castle\\castleShooter");
            texture = manager.UseAsset("level\\castle\\castleShooter");
            return texture;
        }

        public override void Reset()
        {
            Active = false;
            position.X = 0;
            position.Y = 0;
            boundingRectangle.X = 0;
            boundingRectangle.Y = 0;
            if (State == SpriteEnumState.Left)
            {
                speedX = random.Next(5, 7);
                speedY = random.Next(6, 8);
                velocity = 0.2f;
            }
            else
            {
                speedX = random.Next(3, 5);
                speedY = random.Next(6, 8);
                velocity = 0.2f;
            }
        }
    }
}
